package com.hankol.community.adapter

import android.annotation.SuppressLint
import android.app.Activity
import android.view.MotionEvent
import android.view.ViewGroup.MarginLayoutParams
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.MessageBus
import com.baselibrary.bean.BannerConfigListBean
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.utils.*
import com.baselibrary.view.SpacesTopDec
import com.baselibrary.view.banner.Banner
import com.baselibrary.view.banner.BannerConfig
import com.baselibrary.view.banner.Transformer
import com.baselibrary.view.banner.loader.GlideImageLoader
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.community.R
import com.hankol.community.bean.DiscoverMultiBean
import com.hankol.community.bean.PlateVo
import com.hankol.community.bean.QuickLink


/**
 * @author: yuyang
 * @date: 2022/11/4
 * @Des：
 */
class CommunityDiscoverHeadAdapter(data: List<DiscoverMultiBean>, var activity: Activity) : BaseMultiItemQuickAdapter<DiscoverMultiBean, BaseViewHolder>(data) {

    init {
        addItemType(DiscoverMultiBean.TYPE_BANNER, R.layout.item_discover_banner)
        addItemType(DiscoverMultiBean.TYPE_IMPORTANT, R.layout.item_discover_other)
        addItemType(DiscoverMultiBean.TYPE_LOCK, R.layout.item_discover_other)
        addItemType(DiscoverMultiBean.TYPE_EXERCISE, R.layout.item_discover_other)
    }

    override fun convert(helper: BaseViewHolder, item: DiscoverMultiBean) {
        when (item.itemType) {
            DiscoverMultiBean.TYPE_BANNER -> {
                initDiscoverBanner(helper, item)
            }
            DiscoverMultiBean.TYPE_IMPORTANT -> {
                initDiscoverPlate(helper, item, true)
            }
            DiscoverMultiBean.TYPE_LOCK -> {
                initDiscoverLock(helper, item)
            }
            DiscoverMultiBean.TYPE_EXERCISE -> {
                initDiscoverPlate(helper, item, false)
            }
        }
    }


    private fun initDiscoverBanner(helper: BaseViewHolder, item: DiscoverMultiBean) {
        val data = item.getSpecialBean() as BannerConfigListBean
        if (data.bannerConfigs.isNotEmpty()) {
            val mBanner = helper.getView<Banner>(R.id.bn_discover)
            if (data.bannerRule != null) {
                BannerSizeUtil.setBannerSizeValue(mBanner, data.bannerRule!!.wide, data.bannerRule!!.high)
            }
            val urlList: MutableList<String> = ArrayList()
            for (configs in data.bannerConfigs) {
                urlList.add(configs.img)
            }
            mBanner?.let { banner ->
                banner.setImageLoader(GlideImageLoader(4))
                banner.setDelayTime(5000)
                banner.setBannerStyle(BannerConfig.CUSTOM_INDICATOR)
                banner.setBannerAnimation(Transformer.Default)
                banner.setIndicatorGravity(BannerConfig.CENTER)
                banner.setImages(urlList)
                banner.setIndicatorGravity(BannerConfig.CENTER)
                banner.setOnBannerListener { position ->
                    data.bannerConfigs[position].let {
                        if (!DoubleClickHelper.isOnDoubleClick()) {
                            SensorsDataManager.instance.homeBannerClick(it.name)
                            ARouterUtils.bannerJump(activity, it.routeType, it.routeUrl, it.appletId)
                        }
                    }

                }
                banner.start()
            }
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initDiscoverPlate(helper: BaseViewHolder, item: DiscoverMultiBean, isImportant: Boolean) {
        val data = item.getSpecialBean() as PlateVo
        val rvCommon = helper.getView<RecyclerView>(R.id.rv_discover_other)
        rvCommon.setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_DOWN) {
                EventBusUtils.sendNotify1Value(MessageBus.msgId_set_scroll, false)
            } else if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) {
                EventBusUtils.sendNotify1Value(MessageBus.msgId_set_scroll, true)
            }
            false
        }
        if (isImportant) {
            if (data.majorPlateConfig.isNotEmpty()) {
                if (data.majorPlateRule != null) {
                    if (data.majorPlateRule.styleType == 0) {
                        val layoutManager = LinearLayoutManager(mContext)
                        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
                        rvCommon.layoutManager = layoutManager
                        if (rvCommon.itemDecorationCount == 0) {
                            rvCommon.addItemDecoration(SpacesTopDec(ScreenUtils.dip2px(mContext, 8F), true))
                            rvCommon.addItemDecoration(SpacesRightNoEndDec(ScreenUtils.dip2px(mContext, 5F), data.majorPlateConfig.size))
                        }
                    } else {
                        val layoutManager = GridLayoutManager(mContext, (data.majorPlateRule.composingType) + 1)
                        rvCommon.layoutManager = layoutManager
                        if (rvCommon.itemDecorationCount == 0) {
                            rvCommon.addItemDecoration(GridSpacesRightNoEndDec(ScreenUtils.dpToPx(5)))
                            rvCommon.addItemDecoration(SpacesBottomDec(ScreenUtils.dpToPx(5)))
                        }
                        val params = rvCommon.layoutParams
                        //获取view的margin设置参数
                        var marginParams: MarginLayoutParams? = if (params is MarginLayoutParams) {
                            params
                        } else {
                            MarginLayoutParams(params)
                        }
                        marginParams?.setMargins(ScreenUtils.dpToPx(0), ScreenUtils.dpToPx(8), 0, 0)
                    }
                }
            }
        } else {
            if (data.activityPlateConfig.isNotEmpty()) {
                if (data.activityPlateRule != null) {
                    if (data.activityPlateRule.styleType == 0) {
                        val layoutManager = LinearLayoutManager(mContext)
                        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
                        rvCommon.layoutManager = layoutManager
                        if (rvCommon.itemDecorationCount == 0) {
                            rvCommon.addItemDecoration(SpacesTopDec(ScreenUtils.dip2px(mContext, 8F), true))
                            rvCommon.addItemDecoration(SpacesRightNoEndDec(ScreenUtils.dip2px(mContext, 5F), data.activityPlateConfig.size))
                        }
                    } else {
                        val layoutManager = GridLayoutManager(mContext, (data.activityPlateRule.composingType) + 1)
                        rvCommon.layoutManager = layoutManager
                        if (rvCommon.itemDecorationCount == 0) {
                            rvCommon.addItemDecoration(GridSpacesRightNoEndDec(ScreenUtils.dpToPx(5)))
                            rvCommon.addItemDecoration(SpacesBottomDec(ScreenUtils.dpToPx(5)))
                        }
                        val params = rvCommon.layoutParams
                        //获取view的margin设置参数
                        var marginParams: MarginLayoutParams? = if (params is MarginLayoutParams) {
                            params
                        } else {
                            MarginLayoutParams(params)
                        }
                        marginParams?.setMargins(ScreenUtils.dpToPx(0), ScreenUtils.dpToPx(18), 0, 0)
                    }
                }
            }
        }


        val adapter: DiscoverPlateAdapter
        if (isImportant) {
            if (data.majorPlateRule != null) {
                adapter = DiscoverPlateAdapter(R.layout.item_discover_important, data.majorPlateConfig)
                adapter.setShowRule(data.majorPlateRule, true, activity)
                rvCommon.adapter = adapter
            }
        } else {
            if (data.activityPlateRule != null) {
                adapter = DiscoverPlateAdapter(R.layout.item_discover_exercise, data.activityPlateConfig)
                adapter.setShowRule(data.activityPlateRule, false, activity)
                rvCommon.adapter = adapter
            }
        }

    }


    private fun initDiscoverLock(helper: BaseViewHolder, item: DiscoverMultiBean) {
        val datas = item.getSpecialBean() as List<QuickLink>
        val rvCommon = helper.getView<RecyclerView>(R.id.rv_discover_other)
        val layoutManager = LinearLayoutManager(mContext)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        rvCommon.layoutManager = layoutManager
        val adapter = DiscoverLockAdapter()
        adapter.setShowRule(activity)
        rvCommon.adapter = adapter
        adapter.setNewData(datas)
    }


}